package com.longdaji.decoration.ui.captainApply;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.bean.CaptainCheckBean;
import com.longdaji.decoration.ui.creditassess.CreditAssessActivity;
import com.longdaji.decoration.ui.main.MainActivity;
import com.longdaji.decoration.ui.webview.WebViewActivity;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by longtao on 2017/12/19.
 */

public class CaptainApplyActivity extends BaseActivity implements CaptainApplyContract.View {

    private static final String TAG = CaptainApplyActivity.class.getSimpleName();
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_credit_state)
    TextView mTvCreditState;
    @BindView(R.id.tv_taobao_state)
    TextView mTvTaobaoState;
    @BindView(R.id.tv_school_state)
    TextView mTvSchoolState;
    @BindView(R.id.tv_operator_state)
    TextView mTvOperatorState;
    @BindView(R.id.sv_can_apply)
    ScrollView mSvCanApply;
    @BindView(R.id.llyt_not_apply)
    LinearLayout mLlytNotApply;
    @BindView(R.id.tv_apply_status)
    TextView mTvApplyStatus;
    @BindView(R.id.tv_apply_text)
    TextView mTvApplyText;
    @BindView(R.id.tv_hint)
    TextView mTvHint;
    @Inject
    CaptainApplyPresenter mPresenter;

    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, CaptainApplyActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "CaptainApplyActivity onCreate");
        setContentView(R.layout.activity_apply_captain);
        setUnBinder(ButterKnife.bind(this));
        initView();
    }

    private void initView() {
        mPresenter.setView(this);
        mTvTitle.setText("验证");
        getData();
    }

    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        super.onDestroy();
    }

    @OnClick({R.id.iv_back, R.id.rlyt_credit, R.id.rlyt_school, R.id.rlyt_taobao, R.id.rlyt_operator, R.id.btn_cofirm, R.id.btn_pick_goods})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rlyt_credit:
                startCreditAssess();
                break;
            case R.id.rlyt_school:
                startSchoolAssess();
                break;
            case R.id.rlyt_taobao:
                startTaobaoAssess();
                break;
            case R.id.rlyt_operator:
                startOperatorAssess();
                break;
            case R.id.btn_cofirm:
                verifyApplyCaptain();
                break;
            case R.id.btn_pick_goods:
                Intent intent = MainActivity.getStartIntent(this);
                intent.putExtra("id", 1);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void startCreditAssess() {
        Intent intent = CreditAssessActivity.getStartIntent(this);
        startActivityForResult(intent, 4);

    }

    private void verifyApplyCaptain() {
        if (!TextUtils.equals("已认证", mTvSchoolState.getText())) {
            toast("学籍认证失败");
            return;
        }
        if (!TextUtils.equals("已认证", mTvCreditState.getText())) {
            toast("芝麻信用认证失败");
            return;
        }
        if (!TextUtils.equals("已认证", mTvTaobaoState.getText())) {
            toast("淘宝认证失败");
            return;
        }
        if (!TextUtils.equals("已认证", mTvOperatorState.getText())) {
            toast("运营商认证失败");
            return;
        }

        Account account = Account.getInstance();
        mPresenter.applyCaptain(account.getUserid(), account.getName(), account.getDormitoryId(), account.getTelephone());
    }

    private void startOperatorAssess() {
        Intent intent = WebViewActivity.getStartIntent(this);
        intent.putExtra("title", "运营商评估");
        intent.putExtra("auth_type", 1);
        startActivityForResult(intent, 1);
    }

    private void startTaobaoAssess() {
        Intent intent = WebViewActivity.getStartIntent(this);
        intent.putExtra("title", "淘宝授权");
        intent.putExtra("auth_type", 2);
        startActivityForResult(intent, 2);
    }

    private void startSchoolAssess() {
        Intent intent = WebViewActivity.getStartIntent(this);
        intent.putExtra("title", "学籍认证");
        intent.putExtra("auth_type", 3);
        startActivityForResult(intent, 3);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                mTvOperatorState.setText("已认证");
            } else if (requestCode == 2) {
                mTvTaobaoState.setText("已认证");
            } else if (requestCode == 3) {
                mTvSchoolState.setText("已认证");
            } else if (requestCode == 4) {
                if (data.getBooleanExtra("verify_result", false)) {
                    mTvCreditState.setText("已认证");
                } else {
                    mTvCreditState.setText("认证失败");
                }

            }
        }
    }

    @Override
    public void showApplySuccess() {
        toast("申请楼长成功");
        Intent intent = MainActivity.getStartIntent(this);
        intent.putExtra("id", 1);
        startActivity(intent);
    }

    @Override
    public void showApplyFail(String msg) {
        toast(msg);
    }

    @Override
    public void getApplyStatusSuccess(CaptainCheckBean.CaptainCheck captainCheck) {
        if (captainCheck != null) {
            if (TextUtils.equals("0", captainCheck.getIsAudit())) {
                mSvCanApply.setVisibility(View.VISIBLE);
                mLlytNotApply.setVisibility(View.GONE);
            } else {
                mSvCanApply.setVisibility(View.GONE);
                mLlytNotApply.setVisibility(View.VISIBLE);
                if (TextUtils.equals("1", captainCheck.getIsAudit())) {
                    mTvApplyStatus.setText("申请中");
                    mTvApplyText.setText("您的申请正在审核中！");
                    mTvHint.setText("请耐心等待，可以先去逛逛商品");

                } else if (TextUtils.equals("2", captainCheck.getIsAudit())) {
                    mTvApplyStatus.setText("申请成功");
                    mTvApplyText.setText("恭喜你成为楼长！");
                    mTvHint.setText("申请成功，60积分已存入账户");
                } else if (TextUtils.equals("3", captainCheck.getIsAudit())) {
                    mTvApplyStatus.setText("审核未通过");
                    mTvApplyText.setText("请两个月后再次尝试申请！");
                    mTvHint.setText("小主，请不要灰心，先去逛逛商品吧");
                } else if (TextUtils.equals("4", captainCheck.getIsAudit())) {
                    mTvApplyStatus.setText("取消资格");
                    mTvApplyText.setText("请努力提升信誉度！");
                    mTvHint.setText("小主，请不要灰心，先去逛逛商品吧");
                } else {
                }
            }
        }

    }

    @Override
    public void getApplyStatusFail() {
        mSvCanApply.setVisibility(View.VISIBLE);
        mLlytNotApply.setVisibility(View.GONE);
    }

    public void getData() {
        mPresenter.getApplyStatus(Account.getInstance().getUserid());
    }
}
